package com.japstudy.util;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
	
	public static <T> int getMaxPage(List<T> list, int pageDataCount) {
		return (int) Math.ceil((list.size()+0.0)/pageDataCount);
	}
	
	public static <T> List<T> getPageData(List<T> list, int page, int pageDataCount) {
		
		if(page > getMaxPage(list, pageDataCount) || page < 1 || list == null) {
			List<T> result = new ArrayList<T>();
			return result;
		}
		
		int pageFirstDataIndex = (page - 1) * pageDataCount;
		int pageLastDataIndex = (page * pageDataCount) - 1;
		List<T> result = new ArrayList<T>();
		
		for (int i = pageFirstDataIndex, j = list.size(); i <= pageLastDataIndex && i < j; i++) {
			result.add(list.get(i));
		}
		
		return result;
	}
}
